package com.example.widgethost.base;

import android.appwidget.AppWidgetHost;
import android.appwidget.AppWidgetHostView;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProviderInfo;
import android.content.ComponentName;
import android.content.Context;
import android.util.Log;
import android.view.ViewGroup;

import com.example.widgethost.base.cache.CacheInterface;

public abstract class AbstractContainerConstructor<T extends ViewGroup, S extends ViewGroup.LayoutParams, K extends AppWidgetHost> {
    private static final String TAG = "AbstractContainerConstructor";
    private T t;
    private K k;
    protected Context context;
    protected AppWidgetManager appWidgetManager;
    private CacheInterface cacheInterface;

    /**
     * constructor.
     *
     * @param context context
     * @param t       container layout
     */
    public AbstractContainerConstructor(Context context, T t) {
        this.context = context.getApplicationContext();
        this.t = t;
        this.k = contructAppWidgetHost();
        this.appWidgetManager = AppWidgetManager.getInstance(context);
        this.k.startListening();
    }

    /**
     * get container layout params.
     *
     * @param appWidgetProviderInfo app widget provider info
     * @return container layout params
     */
    protected abstract S getContainerLayoutParams(AppWidgetProviderInfo appWidgetProviderInfo);

    /**
     * contruct app widget host.
     *
     * @return app widget host
     */
    protected abstract K contructAppWidgetHost();

    /**
     * add widget to host.
     *
     * @param appWidgetHostView app widget host view
     */
    public void addWdigetToHost(AppWidgetHostView appWidgetHostView) {
        Log.e(TAG, "addWdigetToHost " + appWidgetHostView);
        AppWidgetProviderInfo appWidgetProviderInfo = appWidgetHostView.getAppWidgetInfo();
        ViewGroup.LayoutParams layoutParams = getContainerLayoutParams(appWidgetProviderInfo);
        t.addView(appWidgetHostView, layoutParams);
    }


    /**
     * add app widget from appWidgetProviderInfo.
     *
     * @param appWidgetProviderInfo app widget provider info
     */
    public void addWidget(AppWidgetProviderInfo appWidgetProviderInfo) {
        int newAppWidgetId = k.allocateAppWidgetId();
        ComponentName componentName = appWidgetProviderInfo.provider;
        String packageName = componentName.getPackageName();
        //appWidgetManager.setBindAppWidgetPermission(packageName, true);
        boolean allowedBound = appWidgetManager.bindAppWidgetIdIfAllowed(newAppWidgetId, componentName);
        Log.d(TAG, "addWidget() allowed add appwidget? " + allowedBound + "//newAppWidgetId " + newAppWidgetId);
        if (allowedBound) {
            AppWidgetHostView appWidgetHostView = createWidgetFromId(newAppWidgetId);
            if (cacheInterface != null) {
                cacheInterface.cacheWidget(newAppWidgetId, componentName);
            }
            addWdigetToHost(appWidgetHostView);
        }
    }

    /**
     * add cache inferface.
     *
     * @param cacheInterface cache interface
     */
    public void setCacheInterface(CacheInterface cacheInterface) {
        this.cacheInterface = cacheInterface;
    }

    /**
     * create widgetview from appwidget id.
     *
     * @param appWidgetId widget id
     * @return widget host view
     */
    public AppWidgetHostView createWidgetFromId(int appWidgetId) {
        AppWidgetProviderInfo appWidgetInfo = appWidgetManager.getAppWidgetInfo(appWidgetId);
        AppWidgetHostView hostView = k.createView(context, appWidgetId, appWidgetInfo);
        hostView.setAppWidget(appWidgetId, appWidgetInfo);
        return hostView;
    }
}
